package com.lexue.dao.impl;

import java.util.List;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.hibernate.LockMode;
import org.hibernate.Query;
import org.hibernate.criterion.Example;
import org.springframework.orm.hibernate3.support.HibernateDaoSupport;

import com.lexue.dao.ICity;
import com.lexue.domain.City;

/**
 * A data access object (DAO) providing persistence and search support for City
 * entities. Transaction control of the save(), update() and delete() operations
 * can directly support Spring container-managed transactions or they can be
 * augmented to handle user-managed Spring transactions. Each of these methods
 * provides additional information for how to configure it for the desired type
 * of transaction control.
 * 
 * @see com.lexue.dao.impl.City
 * @author MyEclipse Persistence Tools
 */

public class CityDAO extends HibernateDaoSupport implements ICity {
	private static final Log log = LogFactory.getLog(CityDAO.class);
	// property constants
	public static final String CITY_NAME = "cityName";

	/*
	 * (non-Javadoc)
	 * 
	 * @see com.lexue.dao.impl.ICity#save(com.lexue.domain.City)
	 */
	public void save(City transientInstance) {
		log.debug("saving City instance");
		try {
			getHibernateTemplate().save(transientInstance);
			log.debug("save successful");
		} catch (RuntimeException re) {
			log.error("save failed", re);
			throw re;
		}
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see com.lexue.dao.impl.ICity#delete(com.lexue.domain.City)
	 */
	public void delete(City persistentInstance) {
		log.debug("deleting City instance");
		try {
			getHibernateTemplate().delete(persistentInstance);
			log.debug("delete successful");
		} catch (RuntimeException re) {
			log.error("delete failed", re);
			throw re;
		}
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see com.lexue.dao.impl.ICity#findById(java.lang.Integer)
	 */
	public City findById(java.lang.Integer id) {
		log.debug("getting City instance with id: " + id);
		try {
			City instance = (City) getHibernateTemplate()
					.get("com.lexue.domain.City", id);
			return instance;
		} catch (RuntimeException re) {
			log.error("get failed", re);
			throw re;
		}
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see com.lexue.dao.impl.ICity#findByExample(com.lexue.domain.City)
	 */
	public List findByExample(City instance) {
		log.debug("finding City instance by example");
		try {
			List results = getHibernateTemplate().getSessionFactory()
					.openSession().createCriteria("com.lexue.domain.City").add(
							Example.create(instance)).list();
			log.debug("find by example successful, result size: "
					+ results.size());
			return results;
		} catch (RuntimeException re) {
			log.error("find by example failed", re);
			throw re;
		}
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see com.lexue.dao.impl.ICity#findByProperty(java.lang.String,
	 * java.lang.Object)
	 */
	public List findByProperty(String propertyName, Object value) {
		log.debug("finding City instance with property: " + propertyName
				+ ", value: " + value);
		try {
			String queryString = "from City as model where model."
					+ propertyName + "= ?";
			Query queryObject = getHibernateTemplate().getSessionFactory()
					.openSession().createQuery(queryString);
			queryObject.setParameter(0, value);
			return queryObject.list();
		} catch (RuntimeException re) {
			log.error("find by property name failed", re);
			throw re;
		}
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see com.lexue.dao.impl.ICity#findByCityName(java.lang.Object)
	 */
	public List findByCityName(Object cityName) {
		return findByProperty(CITY_NAME, cityName);
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see com.lexue.dao.impl.ICity#findAll()
	 */
	public List findAll() {
		log.debug("finding all City instances");
		try {
			String queryString = "from City";
			Query queryObject = getHibernateTemplate().getSessionFactory()
					.openSession().createQuery(queryString);
			return queryObject.list();
		} catch (RuntimeException re) {
			log.error("find all failed", re);
			throw re;
		}
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see com.lexue.dao.impl.ICity#merge(com.lexue.domain.City)
	 */
	public City merge(City detachedInstance) {
		log.debug("merging City instance");
		try {
			City result = (City) getHibernateTemplate().merge(detachedInstance);
			log.debug("merge successful");
			return result;
		} catch (RuntimeException re) {
			log.error("merge failed", re);
			throw re;
		}
	}
	
	public void updateCityName(Integer cityId,String cityName){
		City city=(City)getHibernateTemplate().get(City.class, cityId);
		city.setCityName(cityName);
		this.getHibernateTemplate().update(city);
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see com.lexue.dao.impl.ICity#attachDirty(com.lexue.domain.City)
	 */
	public void attachDirty(City instance) {
		log.debug("attaching dirty City instance");
		try {
			getHibernateTemplate().saveOrUpdate(instance);
			log.debug("attach successful");
		} catch (RuntimeException re) {
			log.error("attach failed", re);
			throw re;
		}
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see com.lexue.dao.impl.ICity#attachClean(com.lexue.domain.City)
	 */
	public void attachClean(City instance) {
		log.debug("attaching clean City instance");
		try {
			getHibernateTemplate().lock(instance, LockMode.NONE);
			log.debug("attach successful");
		} catch (RuntimeException re) {
			log.error("attach failed", re);
			throw re;
		}
	}
}